import reqests from "../utils/requests.js"
import {STATUS_CODE} from "../utils/variables.js"

/**
 * 保存收货地址
 * @param {Object} data
 */
export async function addAddressApi(data) {
	const res = await reqests.request({
		url: "/user/address",
		data,
		method: "POST"
	})
	if(res.code!=STATUS_CODE.SUCCESS_CODE){
		uni.showToast({
			title:"添加地址失败，请稍后重试",
			icon:'none',
			duration:2000
		})
		return;
	}
	return res;
}

/**
 * 获取收货地址列表
 */
export async function getAddressListApi() {
	const res = await reqests.request({
		url: "/user/address",
		method: "GET"
	})
	if(res.code!=STATUS_CODE.SUCCESS_CODE){
		uni.showToast({
			title:"获取信息地址失败，请稍后重试",
			icon:'none',
			duration:2000
		})
		return;
	}
	return res.data;
}

/**
 * 获取默认收货地址
 */
export async function getAddressDefaultApi() {
	const res = await reqests.request({
		url: "/user/address/default",
		method: "GET"
	})
	if(res.code!=STATUS_CODE.SUCCESS_CODE){
		uni.showToast({
			title:"获取信息地址失败，请稍后重试",
			icon:'none',
			duration:2000
		})
		return;
	}
	return res.data;
}



/**
 * 根据Id 获取地址信息
 * @param {Object} id
 */
export async function getAddressByIdApi(id) {
	const res = await reqests.request({
		url: `/user/address/${id}/`,
		method: "GET"
	})
	if(res.code!=STATUS_CODE.SUCCESS_CODE){
		uni.showToast({
			title:"获取信息地址失败，请稍后重试",
			icon:'none',
			duration:2000
		})
		return;
	}
	return res.data;
}

/**
 * 修改收货地址
 * @param {Object} data
 */
export async function updateAddressApi(data) {
	const res = await reqests.request({
		url: "/user/address",
		data,
		method: "PUT"
	})
	if(res.code!=STATUS_CODE.SUCCESS_CODE){
		uni.showToast({
			title:"添加地址失败，请稍后重试",
			icon:'none',
			duration:2000
		})
		return;
	}
	return res;
}
